$(function() {

    upadteViewCount();

    detectIsPraise();

    detectIsBasket();

    $('.collect .like').on('click', onFavor);
    $('.collect .join').on('click', onPick);

    var summary = $('#summary').val();
    $('.content .des').html(summary.replace(/\n/ig,'<br/>'));

}());

/**更新浏览量*/
function upadteViewCount() {
    ghttp.post('/menu/viewcount/' + window.menuId, null);
}

/**检查是否已点赞*/
function detectIsPraise() {
    if(gcommon.uid()) {
        ghttp.get('/personal/praise/user/' + gcommon.uid() + '/type/2/sourceId/' + window.menuId, null, function(resp) {
            if(resp.success) {
                if(resp.result) {
                    // 赞过
                    var $like = $('.collect .like');
                    $like.addClass('active');
                    $like.text('取消点赞');
                }
            }
        });
    }
}

/**检查是否在菜篮*/
function detectIsBasket() {
    if(gcommon.uid()) {
        ghttp.get('/personal/basket/user/' + gcommon.uid() + '/type/2/sourceId/' + window.menuId, null, function(resp) {
            if(resp.success) {
                var $join = $('.collect .join');
                if(resp.result && resp.result.length > 0) {
                    // 已在菜篮
                    $join.addClass('active');
                    $join.text('移出菜篮');
                }
            }
        });
    }
}

/***点赞*/
function onFavor() {
    if(gcommon.uid()) {
        var $num = $('.info .love .num');
        var $like = $('.collect .like');
        var count = $num.text()*1;

        if(!$like.hasClass('active')) {
            $like.addClass('active');
            $like.text('取消点赞');
            count ++;
        } else {
            $like.removeClass('active');
            $like.text('点赞');
            count --;
        }
        gloading.start();
        ghttp.post('/personal/praise/user/' + gcommon.uid() + '/type/2/sourceId/' + window.menuId, null, function() {
            $num.text(count);
            gloading.stop();
        });
    }
}

/**
 * 添加到篮子
 */
function onPick() {
    if(gcommon.uid()) {
        var $join = $('.collect .join');

        var mainArray = window.mainStaff.split(',');
        var assistArray = window.assistStaff.split(',');
        var names = [];
        mainArray.forEach(function (item) {
            names.push(item.split(' ')[0]);
        });
        assistArray.forEach(function (item) {
            names.push(item.split(' ')[0]);
        });

        gloading.start();
        ghttp.post('/personal/basket', {
            uid: gcommon.uid(),
            type: 2,
            sid: window.menuId,
            name: names.join(','),
            extra: window.menuName
        }, function(resp) {
            if(!resp.success) {
                gloading.stop();
                return;
            }



            var count = 0;
            if($join.hasClass('active')) {
                $join.removeClass('active');
                $join.text('扔进菜篮');

                count = localStorage.getItem('local-basket-count') || 0;
                if(count > 0) {
                    localStorage.setItem('local-basket-count', count*1-1);
                    if(count*1-1 === 0) {
                        $('.badget').hide();
                    } else {
                        $('.badget').text(count*1-1).show();
                    }
                }
            } else {
                $join.addClass('active');
                $join.text('移出菜篮');

                count = localStorage.getItem('local-basket-count') || 0;
                localStorage.setItem('local-basket-count', count*1+1);
                $('.badget').text(count*1+1).show();
            }

            gloading.stop();
        });
    }
}